import axios from 'axios';
const logout = function () {
  sessionStorage.clear();
};

const instance = axios.create({
  timeout: 20000
});
instance.interceptors.request.use(
  (config: any) => {
    let authToken = sessionStorage.getItem('access_token');
    if (authToken != null && !config.headers.isNotAuth) {
      config.headers.Authorization = `Bearer ` + authToken;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
instance.interceptors.response.use(
  (response: any) => {
    if (response.status === '401') {
      logout();
    }
    return response.data;
  },
  (error) => {
    if (error && error.response) {
      if (error.response.status == '401') {
        logout();
      }
    }

    return Promise.reject(error);
  }
);

export default instance;
